﻿import _ from 'lodash';
import ca from "element-ui/src/locale/lang/ca";

/**
 * 通用js方法封装处理
 * Copyright (c) 2019 zn-admin
 */
// 表单重置
export function resetForm(refName, uploadName) {
	if (this.$refs[refName]) {
		this.$refs[refName].resetFields();
	}

	const upload = uploadName || "upload";
  if (this.$refs[upload]) {
    this.$refs[upload].clearAll();
  }
}

// 回显数据字典
export function selectDictLabel(datas, value) {
	var actions = [];
	Object.keys(datas).some((key) => {
		if (datas[key].dictValue === ('' + value)) {
			actions.push(datas[key].dictLabel);
			return true;
		}
	})
	return actions.join('');
}

// 回显数据字典（字符串数组）
export function selectDictLabels(datas, value, separator) {
	var actions = [];
	var currentSeparator = undefined === separator ? "," : separator;
	var temp = value.split(currentSeparator);
	Object.keys(value.split(currentSeparator)).some((val) => {
		Object.keys(datas).some((key) => {
			if (datas[key].dictValue === ('' + temp[val])) {
				actions.push(datas[key].dictLabel + currentSeparator);
			}
		})
	})
	return actions.join('').substring(0, actions.join('').length - 1);
}

/**
 * 构造树型结构数据
 * @param {*} data 数据源
 * @param {*} id id字段 默认 'id'
 * @param {*} parentId 父节点字段 默认 'parentId'
 * @param {*} children 孩子节点字段 默认 'children'
 * @param {*} rootId 根Id 默认 0
 */
export function handleTree(data, id, parentId, children, rootId) {
	id = id || 'id';
	parentId = parentId || 'parentId';
	children = children || 'children';
	rootId = rootId || "0";
	//对源数据深度克隆
	const cloneData = JSON.parse(JSON.stringify(data));
	//循环所有项
	const treeData = cloneData.filter(father => {
		let branchArr = cloneData.filter(child => {
			//返回每一项的子级数组
			return father[id] === child[parentId]
		});
		branchArr.length > 0 ? father.children = branchArr : '';
		//返回第一层
		return father[parentId] === rootId;
	});
	return treeData !== '' ? treeData : data;
}

export const validateFlowNodes = (rule, value, callback) => {
  if (!value || value.length === 0) {
    callback("请至少添加一个审批节点");
    return;
  }

  _.forEach(value, node => {
    const items = node.items;
    _.forEach(items, item => {
      if (!item.userCode || item.userCode === '') {
        callback(new Error('审批人不能为空'));
        return false;
      }
    });
  });
  callback();
};

export const getTreePath = (treeList, target, idKey = 'id', childrenKey = 'children') => {
  const nodePaths = [];
  try {
    _.forEach(treeList, node => {
      getNodePath(node, target, idKey, childrenKey, nodePaths);
    });
  } catch (e) {

  }
  return nodePaths;
}

export function getNodePath(node, target, idKey, childrenKey, nodePaths) {
  // 这里可以自定义push的内容，而不是整个node,而且这里node也包含了children
  nodePaths.push(node[idKey]);
  // 找到符合条件的节点，通过throw终止掉递归
  if (node[idKey] === target) {
    throw ("over!");
  }
  if (node[childrenKey] && node[childrenKey].length > 0) {
    _.forEach(node[childrenKey], child => {
      getNodePath(child, target, idKey, childrenKey, nodePaths);
    });
    // 当前节点的子节点遍历完依旧没找到，则删除路径中的该节点
    nodePaths.pop();
  } else {
    // 找到叶子节点时，删除路径当中的该叶子节点
    nodePaths.pop();
  }
}
